
package com.example.videotrim.utils;

import com.example.videotrim.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class TrimmerBar extends FrameLayout {
    private static final String TAG = "TrimmerBar";

    private OnTrimBarListener mTrimBarListener;

    private RelativeLayout mTrimBarLayout;

    private ImageView mTrimHandleLeftImageView;

    private ImageView mTrimHandleRightImageView;

    private RelativeLayout.LayoutParams mTrimHandleLeftLayoutParmas;

    private RelativeLayout.LayoutParams mTrimHandleRightLayoutParmas;

    private RelativeLayout.LayoutParams mTrimBarLayoutParmas;

    private int mTrimHandleWidth;

    private int mTrimHandleHeight;

    private int mTrimGaugeFgColor;

    private int mTrimGaugeBgColor;

    private int mTrimPlayPositionColor;

    private int mTrimPlayPosition;

    private int mTrimPlayTime;

    private int mTrimGaugeWidth;

    private int mTrimGaugeHeight;

    private int mTrimGaugeStartPosition;

    private int mTrimGaugeEndPosition;

    private int mTrimGaugeLeftMargin;

    private int mTrimGaugeRightMargin;

    private Paint mTrimBarPaint;

    private int mMediaDuration;

    public TrimmerBar(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);

        mTrimBarListener = null;

        mTrimBarPaint = new Paint();
        mTrimBarPaint.setStyle(Paint.Style.FILL);

        mTrimHandleLeftLayoutParmas = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        mTrimHandleLeftLayoutParmas.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        mTrimHandleRightLayoutParmas = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        mTrimHandleRightLayoutParmas.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

        mTrimBarLayoutParmas = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT);
        mTrimBarLayoutParmas.addRule(RelativeLayout.CENTER_HORIZONTAL, 0);

        mTrimBarLayout = new RelativeLayout(context);
        mTrimBarLayout.setLayoutParams(mTrimBarLayoutParmas);
        addView(mTrimBarLayout);

        mTrimHandleLeftImageView = new ImageView(context);
        mTrimHandleLeftImageView.setImageResource(R.drawable.thumb);

        mTrimHandleRightImageView = new ImageView(context);
        mTrimHandleRightImageView.setImageResource(R.drawable.thumb);

        mTrimHandleHeight = mTrimHandleLeftImageView.getDrawable().getIntrinsicHeight();
        mTrimHandleWidth = mTrimHandleLeftImageView.getDrawable().getIntrinsicWidth();

        mTrimBarLayout.addView(mTrimHandleLeftImageView, mTrimHandleLeftLayoutParmas);
        mTrimBarLayout.addView(mTrimHandleRightImageView, mTrimHandleRightLayoutParmas);

        mTrimGaugeBgColor = 0x44668800;
        mTrimGaugeFgColor = 0xff668800;
        mTrimPlayPositionColor = 0xffededed;

        mTrimPlayPosition = 0;
        mTrimPlayTime = 0;

        mTrimGaugeLeftMargin = mTrimHandleWidth / 2;
        mTrimGaugeRightMargin = mTrimHandleWidth / 2;

        Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay();

        mTrimGaugeWidth = display.getWidth() - (mTrimGaugeLeftMargin + mTrimGaugeRightMargin);
        mTrimGaugeHeight = mTrimHandleHeight / 4;

        mMediaDuration = 0;

        mTrimGaugeStartPosition = 0;
        mTrimGaugeEndPosition = mTrimGaugeWidth;

        mTrimHandleLeftLayoutParmas.leftMargin = mTrimGaugeStartPosition;
        mTrimHandleLeftImageView.setLayoutParams(mTrimHandleLeftLayoutParmas);

        mTrimHandleRightLayoutParmas.leftMargin = mTrimGaugeEndPosition;
        mTrimHandleRightImageView.setLayoutParams(mTrimHandleRightLayoutParmas);

        mTrimHandleLeftImageView.setOnTouchListener(new OnTouchListener() {
            private static final String TAG = "mTrimHandleLeftImageView";

            public boolean onTouch(View v, MotionEvent event) {
                if (mMediaDuration == 0)
                    return false;

                int x = (int) event.getRawX();
                int action = event.getAction();
                Log.v(TAG, "onTouch --> getRawX():" + x + ", getAction():" + action);

                if (action == MotionEvent.ACTION_MOVE) {
                    if (x < mTrimGaugeLeftMargin)
                        x = mTrimGaugeLeftMargin;
                    if (x > mTrimGaugeEndPosition - mTrimGaugeLeftMargin)
                        x = mTrimGaugeEndPosition - mTrimGaugeLeftMargin;
                    mTrimGaugeStartPosition = x - mTrimGaugeLeftMargin;
                    mTrimHandleLeftLayoutParmas.leftMargin = mTrimGaugeStartPosition;
                    mTrimHandleLeftImageView.setLayoutParams(mTrimHandleLeftLayoutParmas);
                    mTrimBarLayout.invalidate();
                    if (mTrimBarListener != null) {
                        int time = (int) (((long) mTrimGaugeStartPosition * mMediaDuration) / mTrimGaugeWidth);
                        mTrimBarListener.onTrimBarChanged(
                                OnTrimBarListener.TRIMBAR_ACTION_LEFT_MOVED, time);
                    }
                } else if (action == MotionEvent.ACTION_DOWN) {
                    if (mTrimBarListener != null) {
                        int time = (int) (((long) mTrimGaugeStartPosition * mMediaDuration) / mTrimGaugeWidth);
                        mTrimBarListener.onTrimBarChanged(
                                OnTrimBarListener.TRIMBAR_ACTION_LEFT_PRESSED, time);
                    }
                } else if (action == MotionEvent.ACTION_UP) {
                    if (mTrimBarListener != null) {
                        int time = (int) (((long) mTrimGaugeStartPosition * mMediaDuration) / mTrimGaugeWidth);
                        mTrimBarListener.onTrimBarChanged(
                                OnTrimBarListener.TRIMBAR_ACTION_LEFT_RELEASED, time);
                    }
                }
                return true;
            }

        });

        mTrimHandleRightImageView.setOnTouchListener(new OnTouchListener() {
            private static final String TAG = "mTrimHandleRightImageView";

            public boolean onTouch(View v, MotionEvent event) {
                if (mMediaDuration == 0)
                    return false;

                int x = (int) event.getRawX();
                int action = event.getAction();
                Log.v(TAG, "onTouch --> getRawX():" + x + ", getAction():" + action);
                if (action == MotionEvent.ACTION_MOVE) {
                    if (x > mTrimGaugeLeftMargin + mTrimGaugeWidth)
                        x = mTrimGaugeLeftMargin + mTrimGaugeWidth;
                    if (x < mTrimGaugeLeftMargin + mTrimGaugeStartPosition + mTrimHandleWidth)
                        x = mTrimGaugeLeftMargin + mTrimGaugeStartPosition + mTrimHandleWidth;
                    mTrimGaugeEndPosition = x - mTrimGaugeLeftMargin;
                    mTrimHandleRightLayoutParmas.leftMargin = mTrimGaugeEndPosition;
                    mTrimHandleRightImageView.setLayoutParams(mTrimHandleRightLayoutParmas);
                    mTrimBarLayout.invalidate();
                    if (mTrimBarListener != null) {
                        int time = (int) (((long) mTrimGaugeEndPosition * mMediaDuration) / mTrimGaugeWidth);
                        mTrimBarListener.onTrimBarChanged(
                                OnTrimBarListener.TRIMBAR_ACTION_RIGHT_MOVED, time);
                    }
                } else if (action == MotionEvent.ACTION_DOWN) {
                    if (mTrimBarListener != null) {
                        int time = (int) (((long) mTrimGaugeEndPosition * mMediaDuration) / mTrimGaugeWidth);
                        mTrimBarListener.onTrimBarChanged(
                                OnTrimBarListener.TRIMBAR_ACTION_RIGHT_PRESSED, time);
                    }
                } else if (action == MotionEvent.ACTION_UP) {
                    if (mTrimBarListener != null) {
                        int time = (int) (((long) mTrimGaugeEndPosition * mMediaDuration) / mTrimGaugeWidth);
                        mTrimBarListener.onTrimBarChanged(
                                OnTrimBarListener.TRIMBAR_ACTION_RIGHT_RELEASED, time);
                    }
                }
                return true;
            }

        });
    }

    public void setMediaDuration(int msec) {
        mMediaDuration = msec;
    }

    public void updatePlayTick(int msec) {
        Log.v(TAG, "updatePlayTick --> msec:" + msec);
        mTrimPlayTime = msec;
        mTrimBarLayout.invalidate();
    }

    public void setTrimLeftTime(int msec) {
        mTrimGaugeStartPosition = (int) (((long) msec * mTrimGaugeWidth) / mMediaDuration);
        mTrimHandleLeftLayoutParmas.leftMargin = mTrimGaugeStartPosition;
        mTrimHandleLeftImageView.setLayoutParams(mTrimHandleLeftLayoutParmas);
        mTrimBarLayout.invalidate();
    }

    public void setTrimRightTime(int msec) {
        mTrimGaugeEndPosition = (int) (((long) msec * mTrimGaugeWidth) / mMediaDuration);
        mTrimHandleRightLayoutParmas.leftMargin = mTrimGaugeEndPosition;
        mTrimHandleRightImageView.setLayoutParams(mTrimHandleRightLayoutParmas);
        mTrimBarLayout.invalidate();
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mTrimBarPaint.setColor(mTrimGaugeBgColor);
        canvas.drawRect(mTrimGaugeLeftMargin, mTrimGaugeHeight * 3 + 2, mTrimGaugeLeftMargin
                + mTrimGaugeWidth, mTrimGaugeHeight * 4 + 2, mTrimBarPaint);

        mTrimBarPaint.setColor(mTrimGaugeFgColor);
        canvas.drawRect(mTrimGaugeLeftMargin + mTrimGaugeStartPosition, mTrimGaugeHeight * 3 + 2,
                mTrimGaugeLeftMargin + mTrimGaugeEndPosition, mTrimGaugeHeight * 4 + 2,
                mTrimBarPaint);

        if (mMediaDuration > 0) {
            mTrimPlayPosition = (mTrimPlayTime * mTrimGaugeWidth) / mMediaDuration;
            mTrimBarPaint.setColor(mTrimPlayPositionColor);
            canvas.drawRect(mTrimGaugeLeftMargin + mTrimPlayPosition - 1, mTrimGaugeHeight * 3 + 2,
                    mTrimGaugeLeftMargin + mTrimPlayPosition + 1, mTrimGaugeHeight * 4 + 2,
                    mTrimBarPaint);
        }
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void setOnTrimBarListener(OnTrimBarListener l) {
        mTrimBarListener = l;
    }

    public interface OnTrimBarListener {
        public static final int TRIMBAR_ACTION_LEFT_PRESSED = 1;

        public static final int TRIMBAR_ACTION_LEFT_MOVED = 2;

        public static final int TRIMBAR_ACTION_LEFT_RELEASED = 3;

        public static final int TRIMBAR_ACTION_RIGHT_PRESSED = 4;

        public static final int TRIMBAR_ACTION_RIGHT_MOVED = 5;

        public static final int TRIMBAR_ACTION_RIGHT_RELEASED = 6;

        boolean onTrimBarChanged(int action, int msec);
    }
}
